# analysis_SI_section_3.R
# This script replicates the analysis in Supporting Information Section 3 of the paper
#   Figure SI.1 through SI.7 and Table SI.2
# Author: Yimeng Li
# Dependencies:
#   Data/Leip2020Pres_by_CD_California.csv
#   Data/CensusDemo_by_CD_California.csv

# Load libraries:
library(dplyr)
library(ggplot2)
library(ggrepel)
library(readr)

# Import data:
Leip2020Pres_by_CD_California <- read_csv("./Replication Package/Data/Leip2020Pres_by_CD_California.csv")
CensusDemo_by_CD_California <- read_csv("./Replication Package/Data/CensusDemo_by_CD_California.csv")

# Prepare data:
Leip2020Pres <- Leip2020Pres_by_CD_California %>%
  mutate(WithTreatment = case_when(
    CD == 32 | CD == 38 | CD == 39 | CD == 40 | CD == 47 ~ 1,
    CD != 32 | CD != 38 | CD != 39 | CD != 40 | CD != 47 ~ 0)
  )

Census_DOF <- CensusDemo_by_CD_California %>%
  mutate(WithTreatment = case_when(
    CD == 32 | CD == 38 | CD == 39 | CD == 40 | CD == 47 ~ 1,
    CD != 32 | CD != 38 | CD != 39 | CD != 40 | CD != 47 ~ 0)
  )

# Figure SI.1
Figure_Turnout <-
  ggplot(Leip2020Pres,  aes(x = reorder(CD, - Turnout), y = 100*Turnout, fill = WithTreatment, label = CD)) +
  geom_bar(stat="identity") +
  theme(legend.position = "none", axis.text.x = element_blank(), axis.title.x = element_blank()) +
  ylab("Percent Turnout 2020") +
  geom_text_repel(data=subset(Leip2020Pres, CD == 32 | CD == 38 | CD == 39 | CD == 40 | CD == 47), nudge_x = 2.5, nudge_y = 5)

# Save Figure
ggsave("./Replication Package/Figure SI.1.pdf", Figure_Turnout, width = 8, height = 6, units = "in", dpi = 600)

# Figure SI.2
Figure_Biden <- ggplot(Leip2020Pres,  aes(x = reorder(CD, - `% Biden`), y = `% Biden`, fill = WithTreatment, label = CD)) +
  geom_bar(stat = "identity") +
  theme(legend.position = "none", axis.text.x = element_blank(), axis.title.x = element_blank()) +
  ylab("Proportion Biden Vote 2020") +
  geom_text_repel(data=subset(Leip2020Pres, CD == 32 | CD == 38 | CD == 39 | CD == 40 | CD == 47), nudge_x = 2.5, nudge_y = .01)

# Save Figure
ggsave("./Replication Package/Figure SI.2.pdf", Figure_Biden, width = 8, height = 6, units = "in", dpi = 600)

# Table SI.2
Table_SI2 <- data.frame(
  variable = c("Percent Biden Vote", "Percent Trump Vote", "Turnout among Registered"),
  Median_w_Treatment = c(median(Leip2020Pres$`% Biden`[Leip2020Pres$WithTreatment == 1]),
                         median(Leip2020Pres$`% Trump`[Leip2020Pres$WithTreatment == 1]),
                         median(Leip2020Pres$Turnout[Leip2020Pres$WithTreatment == 1]))*100,
  Mean_w_Treatment = c(mean(Leip2020Pres$`% Biden`[Leip2020Pres$WithTreatment == 1]),
                       mean(Leip2020Pres$`% Trump`[Leip2020Pres$WithTreatment == 1]),
                       mean(Leip2020Pres$Turnout[Leip2020Pres$WithTreatment == 1]))*100,
  Median_wo_Treatment = c(median(Leip2020Pres$`% Biden`[Leip2020Pres$WithTreatment == 0]),
                          median(Leip2020Pres$`% Trump`[Leip2020Pres$WithTreatment == 0]),
                          median(Leip2020Pres$Turnout[Leip2020Pres$WithTreatment == 0]))*100,
  Mean_wo_Treatment = c(mean(Leip2020Pres$`% Biden`[Leip2020Pres$WithTreatment == 0]),
                        mean(Leip2020Pres$`% Trump`[Leip2020Pres$WithTreatment == 0]),
                        mean(Leip2020Pres$Turnout[Leip2020Pres$WithTreatment == 0]))*100
) %>%
  mutate(across(-variable, ~round(., 2)))

# Save Table
write_csv(Table_SI2, "./Replication Package/Table SI.2.csv")

# Figure SI.3
Figure_Density <- ggplot(Census_DOF,  aes(x=reorder(CD, -PersonsSqMile), y=PersonsSqMile, fill = WithTreatment, label=CD)) +
  geom_bar(stat="identity") +
  theme(legend.position="none", axis.text.x=element_blank(), axis.title.x=element_blank()) +
  ylab("Persons per square mile") +
  geom_text_repel(data=subset(Census_DOF, CD == 32 | CD == 38 | CD == 39 | CD == 40 | CD == 47), nudge_x = 2, nudge_y=350)

# Save Figure
ggsave("./Replication Package/Figure SI.3.pdf", Figure_Density, width = 8, height = 6, units = "in", dpi = 600)

# Figure SI.4
Figure_White <- ggplot(Census_DOF,  aes(x=reorder(CD, -`% White`), y=100*`% White`, fill = WithTreatment, label=CD)) +
  geom_bar(stat="identity") +
  theme(legend.position="none", axis.text.x=element_blank(), axis.title.x=element_blank()) +
  ylab("Percentage White, Non-Hispanic") +
  geom_text_repel(data=subset(Census_DOF, CD == 32 | CD == 38 | CD == 39 | CD == 40 | CD == 47), nudge_x = 2, nudge_y=1.5)

# Save Figure
ggsave("./Replication Package/Figure SI.4.pdf", Figure_White, width = 8, height = 6, units = "in", dpi = 600)

# Figure SI.5
Figure_Black <- ggplot(Census_DOF,  aes(x=reorder(CD, -`% Black`), y=100*`% Black`, fill = WithTreatment, label=CD)) +
  geom_bar(stat="identity") +
  theme(legend.position="none", axis.text.x=element_blank(), axis.title.x=element_blank()) +
  ylab("Percentage Black, Non-Hispanic") +
  geom_text_repel(data=subset(Census_DOF, CD == 32 | CD == 38 | CD == 39 | CD == 40 | CD == 47), nudge_x = 2, nudge_y=1.5)

# Save Figure
ggsave("./Replication Package/Figure SI.5.pdf", Figure_Black, width = 8, height = 6, units = "in", dpi = 600)

# Figure SI.6
Figure_Asian <- ggplot(Census_DOF,  aes(x=reorder(CD, -`% Asian`), y=100*`% Asian`, fill = WithTreatment, label=CD)) +
  geom_bar(stat="identity") +
  theme(legend.position="none", axis.text.x=element_blank(), axis.title.x=element_blank()) +
  ylab("Percentage Asian, Non-Hispanic") +
  geom_text_repel(data=subset(Census_DOF, CD == 32 | CD == 38 | CD == 39 | CD == 40 | CD == 47), nudge_x = 2, nudge_y=1.5)

# Save Figure
ggsave("./Replication Package/Figure SI.6.pdf", Figure_Asian, width = 8, height = 6, units = "in", dpi = 600)

# Figure SI.7
Figure_Hispanic <- ggplot(Census_DOF,  aes(x=reorder(CD, -`% Hispanic`), y=100*`% Hispanic`, fill = WithTreatment, label=CD)) +
  geom_bar(stat="identity") +
  theme(legend.position="none", axis.text.x=element_blank(), axis.title.x=element_blank()) +
  ylab("Percentage Hispanic/Latino") +
  geom_text_repel(data=subset(Census_DOF, CD == 32 | CD == 38 | CD == 39 | CD == 40 | CD == 47), nudge_x = 2, nudge_y=1.5)

# Save Figure
ggsave("./Replication Package/Figure SI.7.pdf", Figure_Hispanic, width = 8, height = 6, units = "in", dpi = 600)
